<% include head.ejs%>
<body>
<header class="t-header">
    <div class="topbar">
        <a href="/prod/index" class="gobakA fl">
            <i class=" icon-angle-left"></i>
        </a>
        <a class="doneA fr">
            删除
        </a>
        <span>购物车</span>
    </div><!-- topbar end -->
</header>

<div class="shopping">

    <div class="shop-group-item">
        <ul>
            <% for(var i in prods){%>
            <li>
                <div class="gwc-info" id="<%= prods[i].ord_id%>">
                    <input type="checkbox" class="check goods-check goodsCheck">
                    <div class="shop-info-img"><a href="#"><img src="<%= prods[i].product_icon%>" /></a></div>
                    <div class="shop-info-text">
                        <h4><%= prods[i].product_name%></h4>
                        <div class="shop-brief"><span>￥<s class="sprice"><%= prods[i].product_price/100%></s></span><span><%= prods[i].value%></span></div>
                        <div class="shop-price">
                            <div class="shop-pices">￥<b class="price"><%= prods[i].prod_price/100%></b></div>
                            <div class="shop-arithmetic">
                                <a href="javascript:;" class="minus">-</a>
                                <span class="num" ><%= prods[i].prod_sum%></span>
                                <a href="javascript:;" class="plus">+</a>
                            </div>
                        </div>
                    </div>
                </div>
            </li>
            <% } %>
        </ul>
    </div>

</div>

<div class="myDeal-listWp <%if(prods!=null){%>hide<%}%>">
    <div id="emptyBox" class="emptyBox">
        <img src="/images/no-more.png">
        <span>没有搜索到任何商品，快去商城挑选心爱的商品吧</span>
        <a class="goShoping-a" href="/prod/index">前去逛逛</a>
    </div>
</div>

<div class="payment-bar">
    <div class="all-checkbox"><input type="checkbox" class="check goods-check" id="AllCheck">全选</div>
    <div class="shop-total">
        <strong>总价：<i class="total" id="AllTotal">0.00</i></strong>
        <span>减免：<i id="sAllTotal">0.00</i></span>
    </div>
    <a href="javascript:;" class="settlement">结算</a>
</div>
<div class="b-menu">
<% include footer.ejs%>
</div>
</body>
</html>
<script>
    $(function(){
        // 数量减
        $(".minus").click(function() {
            var t = $(this).parent().find('.num');
            t.text(parseInt(t.text()) - 1);
            var pid = $(this).parents(".gwc-info").attr("id");
            if (t.text() < 1) {
                delOrder(pid);
                iosOverlay({
                    text: "删除成功!",
                    duration: 2e3,
                    icon: "/images/tipsokico.png"
                });
                setTimeout(function () {
                    location.reload();
                },2000)
                t.text(1);
            }else {
                addSum(pid,-1);
            }
            TotalPrice();
        });
        // 数量加
        $(".plus").click(function() {
            var t = $(this).parent().find('.num');
            t.text(parseInt(t.text()) + 1);
            var pid = $(this).parents(".gwc-info").attr("id");
            if (t.text() <= 1) {
                t.text(1);
            }else {
                addSum(pid,1);
            }
            TotalPrice();
        });

        //删除
        $(".doneA").click(function () {
            var ids = [];
            $(".shop-group-item").each(function() { //循环每个店铺
                $(this).find(".goodsCheck").each(function() { //循环店铺里面的商品
                    if ($(this).is(":checked")) { //如果该商品被选中
                        ids.push($(this).parents(".gwc-info").attr("id"));
                    }
                });
            });
            if(ids.length == 0){
                iosOverlay({
                    text: "请选择删除商品!",
                    duration: 2e3,
                    icon: "/images/erricon.png"
                });
            }else {
                for(var i in ids){
                    delOrder(ids[i]);
                }
                iosOverlay({
                    text: "删除成功!",
                    duration: 2e3,
                    icon: "/images/tipsokico.png"
                });
                setTimeout(function () {
                    location.reload();
                },2000)
            }
        })
        /******------------分割线-----------------******/
        // 点击商品按钮
        $(".goodsCheck").click(function() {
            var goods = $(this).closest(".shop-group-item").find(".goodsCheck"); //获取本店铺的所有商品
            var goodsC = $(this).closest(".shop-group-item").find(".goodsCheck:checked"); //获取本店铺所有被选中的商品
            if (goods.length == goodsC.length) { //如果选中的商品等于所有商品
                $("#AllCheck").prop('checked', false); //else全选按钮不被选中
                TotalPrice();
            } else { //如果选中的商品不等于所有商品
                $("#AllCheck").prop('checked', false); //全选按钮也不被选中
                // 计算
                TotalPrice();
                // 计算
            }
        });
        // 点击全选按钮
        $("#AllCheck").click(function() {
            if ($(this).prop("checked") == true) { //如果全选按钮被选中
                $(".goods-check").prop('checked', true); //所有按钮都被选中
                TotalPrice();
            } else {
                $(".goods-check").prop('checked', false); //else所有按钮不全选
                TotalPrice();
            }
        });
        //计算
        function TotalPrice() {
            var allprice = 0; //总价
            var sallprice = 0; //原总价
            $(".shop-group-item").each(function() { //循环每个店铺
                var oprice = 0; //店铺总价
                var soprice = 0; //店铺总价
                $(this).find(".goodsCheck").each(function() { //循环店铺里面的商品
                    if ($(this).is(":checked")) { //如果该商品被选中
                        var num = parseInt($(this).parents(".gwc-info").find(".num").text()); //得到商品的数量
                        var price = parseFloat($(this).parents(".gwc-info").find(".price").text()); //得到商品的单价
                        var sprice = parseFloat($(this).parents(".gwc-info").find(".sprice").text()); //得到商品的原价
                        var total = price * num; //计算单个商品的总价
                        var stotal = sprice * num; //计算单个商品的总价
                        oprice += total; //计算该店铺的总价
                        soprice += stotal;
                    }
                    //计算所有店铺的总价
                    //$(this).closest(".shop-group-item").find(".ShopTotal").text(oprice.toFixed(2)); //显示被选中商品的店铺总价
                });
                //var oneprice = parseFloat($(this).find(".ShopTotal").text()); //得到每个店铺的总价
                allprice += oprice;
                sallprice += soprice;
            });
            $("#AllTotal").text(allprice.toFixed(2)); //输出全部总价
            $("#sAllTotal").text((sallprice-allprice).toFixed(2)); //输出全部总价
        }

        $('.settlement').click(function () {
            var ids = [];
            $(".shop-group-item").each(function() { //循环每个店铺
                $(this).find(".goodsCheck").each(function() { //循环店铺里面的商品
                    if ($(this).is(":checked")) { //如果该商品被选中
                        ids.push($(this).parents(".gwc-info").attr("id"));
                    }
                });
            });

            if(ids.length == 0){
                iosOverlay({
                    text: "请选择结算商品!",
                    duration: 2e3,
                    icon: "/images/erricon.png"
                });
            }else {
                subOrder(ids);
            }
        })
    });

    function delOrder(ordId) {
        $.post("/order/delOrd",{ordId:ordId},function (data) {
            console.log(data);
        })
    }

    function subOrder(ordIds) {
        $.post("/order/subOrd",{orderIds:ordIds.join(',')},function (data) {
            console.log(data);
            var p = JSON.parse(data);
            if(p.code == 0){
                location.href = p.url;
            }else {
                iosOverlay({
                    text: p.msg,
                    duration: 2e3,
                    icon: "/images/erricon.png"
                });
            }
        })
    }

    function addSum(ordId,sum) {
        $.post("/order/addProdSum",{ordId:ordId,sum:sum},function (data) {
            console.log(data);
        })
    }
</script>